﻿using System;
using System.Data.SqlClient;
using System.Web;
using System.Web.Routing;

namespace GameAdmin
{
    public class Global : System.Web.HttpApplication
    {
        private void RegisterRoutes(RouteCollection routes)
        {
            routes.Ignore("{resource}.axd/{*pathInfo}");

            // list of users
            routes.MapPageRoute("users-paging", "users/{page}", "~/Pages/users.aspx");
            routes.MapPageRoute("users", "users", "~/Pages/users.aspx");

            // editing user
            routes.MapPageRoute("user", "user/{loginName}", "~/Pages/user.aspx");

            // new user
            routes.MapPageRoute("userNew", "userNew", "~/Pages/userNew.aspx");

            // user score
            routes.MapPageRoute("userscore", "score/user-{loginName}", "~/Pages/score.aspx");

            // list of scores
            routes.MapPageRoute("scores-paging", "scores/{page}", "~/Pages/scores.aspx");
            routes.MapPageRoute("scores", "scores", "~/Pages/scores.aspx");

            // list of questions
            routes.MapPageRoute("questions-paging", "questions/{page}", "~/Pages/questions.aspx");
            routes.MapPageRoute("questions", "questions", "~/Pages/questions.aspx");

            // editing question + add
            routes.MapPageRoute("question-edit", "question/{id}", "~/Pages/question.aspx");
            routes.MapPageRoute("question-add", "question", "~/Pages/question.aspx");

            // list of answers
            routes.MapPageRoute("answers", "answers/question-{questionId}", "~/Pages/answers.aspx");
            
            // editing answer + add
            routes.MapPageRoute("answer-add", "answer/question-{questionId}", "~/Pages/answer.aspx");
            routes.MapPageRoute("answer-edit", "answer/{id}", "~/Pages/answer.aspx");  

            // list of levels
            routes.MapPageRoute("levels-paging", "levels/{page}", "~/Pages/levels.aspx");
            routes.MapPageRoute("levels", "levels", "~/Pages/levels.aspx");

            // editing level + add
            routes.MapPageRoute("level-edit", "level/{id}", "~/Pages/level.aspx");
            routes.MapPageRoute("level-add", "level", "~/Pages/level.aspx");
            routes.MapPageRoute("mapdesigner", "mapdesigner/{roundId}", "~/Pages/mapdesigner.aspx");
        }

        protected void Application_Start(object sender, EventArgs e)
        {
            RegisterRoutes(RouteTable.Routes);
        }

        protected void Application_EndRequest(object sender, EventArgs e)
        {
            var dbConnection = HttpContext.Current.Items["myConnection"] as SqlConnection;
            if (dbConnection != null)
                dbConnection.Dispose();
        }
    }
}